package com.yst.metho.web.application.user;

import java.util.Calendar;

import nextapp.echo2.app.Column;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Grid;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Label;
import nextapp.echo2.app.TextField;

import com.yst.suivi.data.rights.Right;
import com.yst.suivi.data.user.User;
import com.yst.suivi.services.exceptions.BusinessErrorCode;
import com.yst.suivi.services.exceptions.BusinessException;

import echopointng.DateField;

public class UserPane extends Column
{
	private TextField mLogin;

	private TextField mNom;

	private TextField mPrenom;

	private TextField mSociete;

	private TextField mCode;

	private TextField mMail;

	private TextField mTjm;

	private String mId = null;

	private DateField mDteFinMission = null;

	public UserPane(User usr)
	{
		super();

		setCellSpacing(new Extent(10));
		setInsets(new Insets(10));
		Grid headerGrid = new Grid();
		headerGrid.setInsets(new Insets(0, 2));
		add(headerGrid);
		Label label;
		label = new Label("Login");
		headerGrid.add(label);
		mLogin = new TextField();
		mLogin.setStyleName("TextField");
		headerGrid.add(mLogin);

		label = new Label("Nom");
		headerGrid.add(label);
		mNom = new TextField();
		mNom.setStyleName("TextField");
		headerGrid.add(mNom);
		label = new Label("Prenom");
		headerGrid.add(label);
		mPrenom = new TextField();
		mPrenom.setStyleName("TextField");
		headerGrid.add(mPrenom);

		label = new Label("Societe");
		headerGrid.add(label);
		mSociete = new TextField();
		mSociete.setStyleName("TextField");
		headerGrid.add(mSociete);

		label = new Label("Code");
		headerGrid.add(label);
		mCode = new TextField();
		mCode.setStyleName("TextField");
		headerGrid.add(mCode);

		label = new Label("Mail");
		headerGrid.add(label);
		mMail = new TextField();
		mMail.setStyleName("TextField");
		headerGrid.add(mMail);

		label = new Label("Date fin ");
		headerGrid.add(label);
		mDteFinMission = new DateField();
		headerGrid.add(mDteFinMission);
		mDteFinMission.setPopUpAlwaysOnTop(true);

		label = new Label("TJM");
		mTjm = new TextField();
		mTjm.setText("0.0");
		// check if the connected user can view financial data
		if (Right.FINANCIAL_VIEW.canDo())
		{
			headerGrid.add(label);
			mTjm.setStyleName("TextField");
			headerGrid.add(mTjm);
		}

		if (null != usr)
		{
			mId = usr.getId();
			mLogin.setEnabled(false);
			mLogin.setText(usr.getLogin());
			mPrenom.setText(usr.getPrenom());
			mNom.setText(usr.getNom());
			mSociete.setText(usr.getSociete());
			mCode.setText(usr.getCode());
			mMail.setText(usr.getMail());
			mTjm.setText(usr.getTjm().toString());
			if (null != usr.getEndDate())
			{
				mDteFinMission.setDisplayedDate(usr.getEndDate());
				mDteFinMission.setSelectedDate(usr.getEndDate());
			}
			else
			{
				mDteFinMission.getTextField().setText("");
			}
		}
		else
		{
			mDteFinMission.getTextField().setText("");
		}
	}

	public UserPane()
	{
		this(null);
	}

	/**
	 * @return the id
	 */
	public String getId()
	{
		return mId;
	}

	/**
	 * @return the login
	 */
	public String getLogin()
	{
		return mLogin.getText();
	}

	/**
	 * @return the nom
	 */
	public String getNom()
	{
		return mNom.getText();
	}

	/**
	 * @return the prenom
	 */
	public String getPrenom()
	{
		return mPrenom.getText();
	}

	/**
	 * @return the societe
	 */
	public String getSociete()
	{
		return mSociete.getText();
	}

	/**
	 * @return the Airbus ID
	 */
	public String getCode()
	{
		return mCode.getText();
	}

	/**
	 * @return the mail
	 */
	public String getMail()
	{
		return mMail.getText();
	}

	/**
	 * @return the tjm
	 */
	public Float getTjm()
	{
		Float tjm;
		try
		{
			tjm = Float.valueOf(mTjm.getText());
		}
		catch (NumberFormatException e)
		{
			String[] params1 = new String[0];
			String[] params2 =
			{ "TJM" };
			throw new BusinessException(BusinessErrorCode.FIELD_SHOULD_BE_NUMBER, params1, params2);
		}
		return tjm;
	}

	/**
	 * Date de fin de l'utilisateur
	 * 
	 * @return
	 */
	public Calendar getDateFin()
	{
		Calendar retour = null;
		if (!mDteFinMission.getTextField().getText().equalsIgnoreCase(""))
		{
			retour = mDteFinMission.getSelectedDate();
		}
		return retour;
	}
}
